import pandas as pd
from collections import Counter


### 画图
#from pyecharts import options as opts
#from pyecharts.globals import ThemeType
#from pyecharts.charts import Bar
from pyecharts import Pie

#读入数据
data = pd.read_excel("奔跑吧嘉宾名单-李运辰.xls")
name = data['名字'].tolist()
types = data['明星类型'].tolist()
print(name)
print(types)

### 统计每一位嘉宾参加次数排名（取前15）
def an1():
    result = Counter(name)
    # 排序
    d = sorted(result.items(), key=lambda x: x[1], reverse=True)
    name_key = [d[i][0] for i in range(0,16)]
    values = [d[i][1] for i in range(0,16)]
    print(name_key)
    print(values)


    # 链式调用
    c = (
        Bar(
            init_opts=opts.InitOpts(  # 初始配置项
                theme=ThemeType.MACARONS,
                animation_opts=opts.AnimationOpts(
                    animation_delay=1000, animation_easing="cubicOut"  # 初始动画延迟和缓动效果
                ))
        )
            .add_xaxis(xaxis_data=name_key)  # x轴
            .add_yaxis(series_name="统计每一位嘉宾参加次数排名（取前15）", y_axis=values)  # y轴
            .set_global_opts(
            title_opts=opts.TitleOpts(title='', subtitle='',  # 标题配置和调整位置
                                      title_textstyle_opts=opts.TextStyleOpts(
                                          font_family='SimHei', font_size=25, font_weight='bold', color='red',
                                      ), pos_left="90%", pos_top="10",
                                      ),
            xaxis_opts=opts.AxisOpts(name='嘉宾', axislabel_opts=opts.LabelOpts(rotate=45)),
            # 设置x名称和Label rotate解决标签名字过长使用
            yaxis_opts=opts.AxisOpts(name='次数'),

        ).render("统计每一位嘉宾参加次数排名（取前15）.html")
    )


### 嘉宾职业类型统计
def an2():
    ### 嘉宾职业类型
    name = ['演员','歌手','主持人','模特','主持人','运动员','舞者','制片人','赛车手','经纪人']
    ### 初始化为0
    value = [0,0,0,0,0,0,0,0,0,0]
    for i in types:
        for j in range(0,len(name)):
            if name[j] in i:
                value[j] = value[j] +1

    print(name)
    print(value)
    pie = Pie("嘉宾职业类型统计",title_pos='center')
    pie.add(
        "",
        name,
        value,
        radius=[40, 75],
        label_text_color=None,
        is_label_show=True,
        is_more_utils=True,
        legend_orient="vertical",
        legend_pos="left",
    )
    pie.render(path="嘉宾职业类型统计.html")




#an1()
an2()